import { CacheService } from './../../../service/cache/cache.service';
import { FocusService } from './../../../service/focus/focus.service';
import { GoodsCateService } from './../../../service/goods-cate/goods-cate.service';
import { ResponseInterceptor } from './../../../interceptor/response/response.interceptor';
import { NavService } from './../../../service/nav/nav.service';
import { Controller, Get, UseInterceptors } from '@nestjs/common';
import config from 'src/config/config';
import { ShopSettingService } from 'src/service/shop-setting/shop-setting.service';

@Controller(`${config.shopApiPath}/index`)
@UseInterceptors(ResponseInterceptor)
export class IndexController {
    constructor(
        private readonly navService: NavService,
        private readonly goodsCateService: GoodsCateService,
        private readonly focusService: FocusService,
        private readonly shopSettingService: ShopSettingService,
        private readonly cacheService: CacheService,
    ) {}
    @Get()
    async find() {
        let navs = await this.cacheService.get('navs');
        if (!navs) {
            navs = await this.navService.find({ position: 1, status: 1 });
            await this.cacheService.set('navs', navs, 60 * 60);
        }
        let centerNavs = await this.cacheService.get('centerNavs');
        if (!centerNavs) {
            centerNavs = await this.navService
                .getModel()
                .aggregate([
                    { $sort: { sort: -1, createdTime: -1 } },
                    { $match: { position: 2, status: 1 } },
                    { $lookup: { localField: 'relation', foreignField: '_id', from: 'goods', as: 'relation' } },
                ]);
            await this.cacheService.set('centerNavs', centerNavs, 60 * 60);
        }

        let bottomNavs = await this.cacheService.get('bottomNavs');
        if (!bottomNavs) {
            bottomNavs = await this.navService.find({ position: 3, status: 1 });
            await this.cacheService.set('bottomNavs', bottomNavs, 60 * 60);
        }
        let categories = await this.cacheService.get('categories');
        if (!categories) {
            categories = await this.goodsCateService.getModel().aggregate([
                {
                    $lookup: {
                        localField: '_id',
                        foreignField: 'pid',
                        as: 'children',
                        from: 'goods_cate',
                    },
                },
                {
                    $lookup: {
                        localField: '_id',
                        foreignField: 'cate_id',
                        as: 'relations',
                        from: 'goods',
                    },
                },
            ]);
            await this.cacheService.set('categories', categories, 60 * 60);
        }

        let focus = await this.cacheService.get('focus');
        if (!focus) {
            focus = await this.focusService.find();
            await this.cacheService.set('focus', focus, 60 * 60);
        }
        let config = await this.cacheService.get('config');
        if (!config) {
            config = await this.shopSettingService.getModel().aggregate([
                {
                    $lookup: {
                        localField: 'main_cate_list',
                        foreignField: '_id',
                        as: 'main_cate_list',
                        from: 'goods_cate',
                        pipeline: [
                            {
                                $lookup: {
                                    localField: '_id',
                                    foreignField: 'cate_id',
                                    as: 'goods',
                                    from: 'goods',
                                    pipeline: [{ $match: { is_best: 1 } }, { $limit: 8 }],
                                },
                            },
                        ],
                    },
                },
            ]);
            await this.cacheService.set('config', config, 60 * 60);
        }
        return {
            navs,
            categories,
            focus,
            config,
            centerNavs,
            bottomNavs,
        };
    }
    @Get('/config')
    async getConfig() {
        const configResult = await this.shopSettingService.find();
        if (configResult.length) return configResult[0];
        return {};
    }
}
